<?php
/**
 * @Author:      lyt
 * @DateTime:  2017-05-18 11:56:50
 * @Description: Description
 */
class AcountUserUndo extends Eloquent
{
    protected $table = "acount_user_undo_record";

    /**
     * @Author:      lyt
     * @DateTime:  2017-05-18 12:09:06
     * @Description: 用于去掉支付记录的使用
     */
    public static function cancel_aur($user_id,$aurr_id,$create_type,$create_by,$au_id,$desc){
        try{
            return DB::transaction(function() use ($user_id,$aurr_id,$create_type,$create_by,$au_id,$desc){
                //交易流水
                // $tran_no = date('Ymd',time()).Common::generateCode();
                $tran_no = date('YmdHis',time()).Common::generateCode(8);
                $aur=AcountUserRecharge::where("id",$aurr_id)->lockForUpdate()->first();

                //记录充值撤销记录
                $auu=new AcountUserUndo();
                $auu->user_id=$user_id;
                $auu->aurr_id=$aurr_id;
                $auu->pay_type=$aur->pay_type;
                $auu->amount=$aur->amount;
                $auu->status=1;
                $auu->create_type=$create_type;
                $auu->create_by=$create_by;
                $auu->tran_no=$tran_no;
                $auu->shop_id=$aur->shop_id;
                $auu->implement_time=Common::currentTimeString();
                $auu->au_id=$au_id;
                $auu->desc=$desc;
                $auu->save();

                if($aur->sources==2){//说明支付途径是 pos 撤销 修改只修改充值记录的的 可用余额
                  $wr=WposRecord::where("type",1)->where("buss_id",$aur->id)->first();
                  $p_r=Pos::statusOrder($wr->id);
                  if($p_r->status==0){
                    if($p_r->data->trade_status=="REFUND"&&$p_r->data->refund_amount==$aur->amount){
                      AcountUserUndo::where("id",$auu->id)->update(array("status"=>1,"tran_no"=>$tran_no,"implement_time"=>Common::currentTimeString()));
                      $wr->trade_status="REFUND";
                      $wr->pay_info=$p_r->data->pay_info;
                      $wr->return_amont=$aur->amount;
                      $wr->save();
                      
                      //调整各种账务记录
                      $uad=UserAccountDeatails::where('account_no',$aur->account_no)->first();
                      Account::record($aur->user_id,$aur->shop_id,$uad->acount_type,$tran_no,2,207,$aur->amount,'充值撤销支出,撤销记录：'.$auu->id."充值记录：".$aur->id);
                      //记录了用户的充值消费记录
                      $aurs=new AcountUserRechargeSpend;
                      $aurs->aurr_id=$aur->id;
                      $aurs->user_id=$aur->user_id;
                      $aurs->buss_id=$auu->id;
                      $aurs->type=3;//撤销充值消费
                      $aurs->amount=$aur->amount;
                      $aurs->status=1;
                      $aurs->tran_no=$tran_no;
                      $aurs->shop_id=$aur->shop_id;
                      $aurs->implement_time=date("Y-m-d H:i:s",time());
                      $aurs->save();
                      //更改原充值记录
                      $aur->current_amount=0;
                      $aur->status=6;
                      $aur->save();
                      return array('code' => 0, 'msg' => 'ok', 'result' => array("auu_id"=>$auu->id));
                    }
                  }
                  $p_r=Pos::refundOrder($wr->device_en,$wr->cashier_trade_no);
                  if($p_r->status==0){
                    AcountUserUndo::where("id",$auu->id)->update(array("status"=>5,"tran_no"=>"","implement_time"=>null));
                  }else{
                    DB::rollback();
                    $response=Lang::get("scs_error.user_not_car_cancel_error_40117");
                    $response["msg"]=$response["msg"].$p_r->info;
                    return $response;
                  }
                }else{
                  if($aur->pay_type==12){//划拨
                    BeginBalanceRecharge::where("aurr_id",$aur->id)->update(array("status"=>6));
                  }else if($aur->pay_type==13){//保险
                    AcountInsurance::where("aurr_id",$aur->id)->delete();
                  }
                  //调整各种账务记录
                  $uad=UserAccountDeatails::where('account_no',$aur->account_no)->first();
                  Account::record($user_id,$aur->shop_id,$uad->acount_type,$tran_no,2,207,$aur->amount,'充值撤销支出,撤销记录：'.$auu->id."充值记录：".$aur->id);
                  //记录了用户的充值消费记录
                  $aurs=new AcountUserRechargeSpend;
                  $aurs->aurr_id=$aur->id;
                  $aurs->user_id=$user_id;
                  $aurs->buss_id=$auu->id;
                  $aurs->type=3;//撤销充值消费
                  $aurs->amount=$aur->amount;
                  $aurs->status=1;
                  $aurs->tran_no=$tran_no;
                  $aurs->shop_id=$aur->shop_id;
                  $aurs->implement_time=date("Y-m-d H:i:s",time());
                  $aurs->save();
                }                         
               	//更改原充值记录
               	$aur->current_amount=0;
               	$aur->status=6;
               	$aur->save();
               	

                
                return array('code' => 0, 'msg' => 'ok', 'result' => array("auu_id"=>$auu->id));
            });
            // return is_null($exception) ? true : $exception;
        } catch (Exception $e) {
            Log::error("error", array("system" => "Account" ,"event" => "cancel_aur" ,"exception" => $e));
            return false;
        }
    }

}